import QtQuick 1.1
import com.nokia.symbian 1.1

Page {
    id: mainPage

    ListView {
        model: trackModel
        anchors.fill: parent
        delegate: Item {
            width: mainPage.width; height: colItem.height;

            Rectangle {
                id: rectColor
                anchors.fill: parent
                color: (index % 2 === 0 ? "#3e0f0f" : platformStyle.colorNormalDark)
                radius: 10
            }

            MeuMouseArea {
                anchors.fill: parent

                onCliqueUnico: {
                    pageStack.push(Qt.resolvedUrl("DetalhePage.qml"));
                    bancoCare.CarregarDetalheTrack(id);
                }
                onCliqueDuplo:  {
                    dlgDescricao.abrirDlg(id, descricao)
                }
            }

            Column {
                id: colItem
                width: parent.width * 0.80
                spacing: 4

                Text {
                    width: parent.width
                    text: rastreio
                    color: platformStyle.colorNormalLight
                    font { pixelSize: 20; }
                    anchors { left: parent.left; leftMargin: 10 }
                    wrapMode: Text.WordWrap
                }

                Rectangle {
                    id: rectDescricao
                    width: parent.width - rectDescricao.left - 14; height: col.height
                    color: platformStyle.colorNormalDark
                    border { width: 2 }
                    anchors { left: parent.left; leftMargin: 10 }
                    radius: 10

                    Column {
                        id: col
                        width: parent.width;

                        Row {
                            id: rwDescricao
                            spacing: 4
                            width: parent.width;

                            Image {
                                id: imgNota
                                width: 48; height: 48;
                                source: "qrc:///nota"

                                MouseArea {
                                    anchors.fill: parent
                                    onDoubleClicked: {
                                        dlgDescricao.abrirDlg(id, descricao)
                                    }
                                }
                            }

                            Text {
                                width: parent.width - imgNota.width - rwDescricao.spacing
                                text: descricao
                                color: platformStyle.colorNormalLight
                                font { pixelSize: 16 }
                                wrapMode: Text.WordWrap
                            }
                        }
                    }
                }
                Rectangle {
                    width: parent.width; height: 20
                    color: rectColor.color
                }
            }
            Item {
                anchors { left: colItem.right; top: parent.top; right: parent.right; bottom: parent.bottom}

                CheckBox {
                    width: 48; height: 48
                    checked: selecionado
                    anchors.centerIn: parent
                    onClicked: {
                         trackModel.Marcar(id, !selecionado);
                    }
                }
            }
        }
    }

    Dialog {
        id: dlgDescricao

        property int idTrack : -1
        property alias novaDescricao : txtNovoDescricao.text

        function abrirDlg(idTrack, descricao) {
            dlgDescricao.idTrack = idTrack;
            dlgDescricao.novaDescricao = descricao;
            dlgDescricao.open();
        }

        title: HeaderDialogs {
            texto: qsTr("_alterar_lembrete")
            icone: "qrc:///nota"
        }

        content: Item {
            width: parent.width; height: parent.height

            TextField {                
                id: txtNovoDescricao
                width: parent.width * 0.9
                placeholderText: qsTr("_lembrete")
                maximumLength: 50
                anchors { centerIn: parent; margins: 10 }
            }
        }

        onStatusChanged: {
            if (status == DialogStatus.Open) {
                txtNovoDescricao.focus = true;
                txtNovoDescricao.selectAll();
            }
        }

        buttons: ButtonRow {
            width: parent.width
            Button {
                text: qsTr("_ok")
                onClicked: {
                    trackModel.AtualizarTrack(dlgDescricao.idTrack, dlgDescricao.novaDescricao);
                    dlgDescricao.close();                    
                }
            }
            Button {
                text: qsTr("_cancelar")
                onClicked: dlgDescricao.close();
            }
        }
    }

    Component.onCompleted: {
        bancoCare.CarregarTracks();
    }

    Column {
        width: childrenRect.width
        anchors.centerIn: parent
        visible: trackModel.qtdItens === 0

        Text {
            color: platformStyle.colorNormalLight
            text: qsTr("_nenhum_rastreio")
        }
        Text {
            color: platformStyle.colorNormalLight
            text: qsTr("_adicione_rastreio")
        }
    }
}
